14. Multiple Outputs

Functions with More than One Output

In Python, you can write a function that has multiple outputs. For example,

## Python Code
def distance(velocity, time_elapsed):
    return velocity * time_elapsed, velocity / 2

would output both velocity * time_elapsed and velocity / 2.

In C++, functions can only have one output. There are work-arounds, but these work-arounds go beyond the scope of this module.

C++ Tip: Function Declarations

You do not have to put the function declaration at the top of your code to get a working solution. Much like how you can declare and define a variable simultaneously, int x = 5;, you can also declare and define a function simultaneously.

The following code would work as well:

// C++ code
float distance(float velocity, float time_elapsed) {
    return velocity * time_elapsed;
}

int main() {

    std::cout << distance(5, 4) << std::endl;
    std::cout << distance(12.1, 7.9) << std::endl;

    return 0;
}

But note that you have to define your function before the main() function not after; otherwise your code would try to call the distance() function but not have a definition for the function.

However, we encourage you to always declare your functions before main() and define them after main. In the next lesson in the nanodegreee called practical C++, you will learn why; declaring and defining your functions separately helps keep your code organized as your programs become more complex.